﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obj;
using BLL;

public partial class AreaRestrita_Marca : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ValidarUsuario();
        if (!IsPostBack)
        {
            List<objMarca> lista = new List<objMarca>();
            BLLMarca regra = new BLLMarca();
            lista = regra.lstMarca();
            gdvMarca.DataSource = lista;
            gdvMarca.DataBind();
        }
    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
        Response.Redirect("MarcaForm.aspx", true);
    }
    protected void gdvMarca_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            objMarca Marca = (objMarca)e.Row.DataItem;
            ImageButton imgEditar = (ImageButton)e.Row.FindControl("imgEditar");
            imgEditar.CommandArgument = Marca.MarcaID.ToString();

            ImageButton imgExcluir = (ImageButton)e.Row.FindControl("imgExcluir");
            imgExcluir.CommandArgument = Marca.MarcaID.ToString();
        }
    }
    protected void gdvMarca_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Excluir")
        {
            BLLMarca regra = new BLLMarca();
            objMarca Marca = new objMarca(Convert.ToInt32(e.CommandArgument));
            regra.Save(Marca, action.DELETE);
            
            ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "ToggleScript",
            "alert('Marca excluída com sucesso!');window.location.href='Marca.aspx'",
            true);
        }
        else if (e.CommandName == "Editar")
        {
            Session["MarcaID"] = e.CommandArgument;
            Response.Redirect("MarcaForm.aspx", true);
        }
    }
    protected void btnFiltrar_Click(object sender, EventArgs e)
    {
        objMarca Marca = new objMarca(txtMarca.Text);
        BLLMarca Regra = new BLLMarca();

        List<objMarca> Marcas = Regra.lstMarca(Marca);

        gdvMarca.DataSource = Marcas;
        gdvMarca.DataBind();
    }
}